package com.cos.shop.web.controller;

import com.cos.shop.common.Const;
import com.cos.shop.entity.Address;
import com.cos.shop.entity.User;
import com.cos.shop.services.AddressServices;
import com.cos.shop.services.impl.AddressServicesImpl;
import com.cos.shop.util.StringUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author: COS
 * @time: 2022/2/24 14:31
 * @description:
 */
@WebServlet("/add")
public class AddressController extends BaseServlet {
    private AddressServices as = new AddressServicesImpl();
    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/2/24 15:27
     * @description: 设置地址为默认地址
     */
    public String setDefaultAddress(HttpServletRequest request, HttpServletResponse response){
        int aid = StringUtils.toInt(request.getParameter("aid"));
        User u = (User) request.getSession().getAttribute(Const.IS_LOGIN);
        //访问业务
        boolean ans = as.setDefaultAddress(aid,u.getUid());
        //回到地址页面
        return Const.REDIRECT+"add?type=show";
    }


    /**
     *
     * @author: COS
     * @return:  
     * @time: 2022/2/24 15:20
     * @description: 
     */
    public String update(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        //1 获取用户提交数据
        Map<String, String[]> map = request.getParameterMap();
        //创建需要封装数据的对象
        Address a = new Address();
        //工具类
        BeanUtils.populate(a, map);
        //访问业务层操作更新
        boolean ans = as.update(a);
        //重定向到获取地址数据的地方
        return Const.REDIRECT+"add?type=show";
    }


    /**
     *
     * @author: COS
     * @return:
     * @time: 2022/2/24 15:07
     * @description: 删除地址
     */
    public String delete(HttpServletRequest request, HttpServletResponse response){
        //1获取用户提交数据
        int aid= StringUtils.toInt(request.getParameter("aid"));
        //2 业务层处理业务
        boolean ans = as.delete(aid);
        //3 回到地址页面
        if (ans) {
            return Const.SUCCESS;
        }
        return Const.FAIL;
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/2/24 14:43
     * @description:
     */
    public String insertAddress(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = request.getParameterMap();
        //创建需要封装数据的对象
        Address a = new Address();
        //工具类
        BeanUtils.populate(a, map);
        //获取用户保存登录数据
        User u = (User) request.getSession().getAttribute(Const.IS_LOGIN);
        //用户主键封装到地址对象中
        a.setUid(u.getUid());
        //访问业务层操作新增
        as.insertAddress(a);
        return Const.REDIRECT+"add?type=show";
    }

    /**
     * @author: COS
     * @return:
     * @time: 2022/2/24 14:31
     * @description: 展示用户的收货地址数据
     */
    public String show(HttpServletRequest request, HttpServletResponse response) {
        //1 直接获取用户登录数据
        User u = (User) request.getSession().getAttribute(Const.IS_LOGIN);
        //2 访问业务层获取用户的地址数据
        List<Address> addList = as.getAddressListByUid(u.getUid());
        //3 保存数据到request作用域
        request.setAttribute("addList", addList);
        //4 转发
        return Const.FORWARD + "self_info.jsp";
    }

}
